# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=imagemagick
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=7.0.1.10
pkgrel=1
pkgdesc="An image viewing/manipulation program (mingw-w64)"
arch=('any')
url="https://www.imagemagick.org/"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-cairo"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-ghostscript"
             "${MINGW_PACKAGE_PREFIX}-librsvg"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-openexr"
             "${MINGW_PACKAGE_PREFIX}-pango"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
            )
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-djvulibre"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-jbigkit"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-liblqr"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libtool"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         #"${MINGW_PACKAGE_PREFIX}-perl"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
        )
optdepends=("${MINGW_PACKAGE_PREFIX}-ghostscript: for Ghostscript support"
            "${MINGW_PACKAGE_PREFIX}-openexr: for OpenEXR support"
            "${MINGW_PACKAGE_PREFIX}-librsvg: for SVG support"
            "${MINGW_PACKAGE_PREFIX}-libxml2: for XML support"
            #"${MINGW_PACKAGE_PREFIX}-jasper: for JPEG-2000 support"
            #"${MINGW_PACKAGE_PREFIX}-libpng: for PNG support"
            #"${MINGW_PACKAGE_PREFIX}-libtiff: for PNG support"
            #"${MINGW_PACKAGE_PREFIX}-libwebp: for WEBP support"
            )
options=('staticlibs' 'strip' '!debug' 'libtool')
source=(https://www.imagemagick.org/download/ImageMagick-${pkgver%.*}-${pkgver##*.}.tar.xz{,.asc}
        ImageMagick-7.0.1.3-mingw.patch
        001-7.0.1.3-relocate.patch
        002-7.0.1.3-build-fixes.patch
        003-7.0.1.8-cplusplus-fixes.patch
        winpath.patch)
sha256sums=('4a17e54cd6b5f5fd0cea462dbe0cc1b74a91730d08e1d8edc4f539066feac8b8'
            'SKIP'
            '9e945a313f5da2962978ff7026c0cf93c0365afaf14c0f0e3223ce7917d81d2c'
            '5e75b298bbd3d5119a4391f65d4974a3f9edce51063a64ef2bd36f8cae0cdb6a'
            '69797713782d0a3634be8a53d6f67fd23bb55479b14d8d13f2499c6a474cd96c'
            'f628755b79753c00a22471a9562f5abcba67702f2156b38aa0e9b83d3644960a'
            'fade8e9e2f6ebfdd2c82d0ebca8d052a5415c63d7b783c80bb6de14e8802eeed')
#Lexie Parsimoniae (ImageMagick code signing key) <lexie.parsimoniae@imagemagick.org>
validpgpkeys=('D8272EF51DA223E4D05B466989AB63D48277377A')

prepare() {
  cd ImageMagick-${pkgver%.*}-${pkgver##*.}
  rm -f MagickCore/pathtools{.c,.h} > /dev/null 2>&1 || true
  patch -p1 -i ${srcdir}/ImageMagick-7.0.1.3-mingw.patch
  patch -p1 -i ${srcdir}/001-7.0.1.3-relocate.patch
  patch -p1 -i ${srcdir}/002-7.0.1.3-build-fixes.patch
  patch -p1 -i ${srcdir}/003-7.0.1.8-cplusplus-fixes.patch
  #patch -p1 -i ${srcdir}/winpath.patch
  autoreconf -fi
}

build() {
  export lt_cv_deplibs_check_method='pass_all'
  [[ $CARCH = "i686" ]] && EXTRAOPTS="--with-gcc-arch=i686"
  [[ $CARCH = "x86_64" ]] && EXTRAOPTS="" #EXTRAOPTS="--with-gcc-arch=x86-64"

  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  # We can't use Magic++ portion due to a bug:
  # https://github.com/ImageMagick/ImageMagick/issues/188
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}
  ../ImageMagick-${pkgver%.*}-${pkgver##*.}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-deprecated \
    --enable-legacy-support \
    --enable-hdri \
    --with-djvu \
    --with-fftw \
    --with-gslib \
    --without-gvc \
    --with-lcms2 \
    --with-lqr \
    --with-modules \
    --with-openexr \
    --with-openjp2 \
    --enable-opencl \
    --with-rsvg \
    --with-webp \
    --with-wmf \
    --with-xml \
    --without-autotrace \
    --without-dps \
    --without-fpx \
    --with-jbig \
    --without-ltdl \
    --without-perl \
    --without-x \
    --with-magick-plus-plus \
    --with-windows-font-dir=c:/Windows/fonts \
    $EXTRAOPTS \
    CFLAGS="${CFLAGS}" CPPFLAGS="${CPPFLAGS}" LDFLAGS="${LDFLAGS}"

  if check_option "debug" "y"; then
    MAKE_VERBOSE="V=1"
  fi
  make ${MAKE_VERBOSE}

    #--enable-opencl \
    # --with-gs-font-dir=${MINGW_PREFIX}/ \
    # --with-dejavu-font-dir= \
    #--with-gslib \
    #--with-gs-font-dir=/usr/share/fonts/Type1
    #--with-perl-options="INSTALLDIRS=vendor"
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  if check_option "debug" "y"; then
    MAKE_VERBOSE="V=1"
  fi
  make -j1 DESTDIR="${pkgdir}" install ${MAKE_VERBOSE}

  #find . -name "*.xml" -exec sed -i "s/${MINGW_PREFIX}/newWord/g" '{}' \;

  install -Dm644 ${srcdir}/ImageMagick-${pkgver%.*}-${pkgver##*.}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 ${srcdir}/ImageMagick-${pkgver%.*}-${pkgver##*.}/NOTICE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/NOTICE"
}
